<?php
namespace app\yunda\model;

use app\yunda\admin\ScoreOrder;
use think\Model;
use traits\model\SoftDelete;
/**
 * 大转盘表
 * @package app\yunda\model
 */
class Active extends Model
{
    use SoftDelete;
    protected $table = 'dp_yd_active';
    protected $autoWriteTimestamp = true;

    //抽奖
    public static function doLuck()
    {
        //查询所有奖品
        $list=self::where(['status'=>1])->select();
        //对象转数组
        $list = collection($list)->toArray();
        //先把所有奖品和权重关联起来
        $arr=[];
        foreach ($list as &$v1){
            $arr[$v1['id']]=$v1['chance'];
        }
        asort($arr);
        $rid=get_rand($arr);
        //$res['yes'] = $list[$rid-1]['active_title']; //中奖项
        $res['yes'] =$list[$rid-1]; //中奖项
        unset($list[$rid-1]); //将中奖项从数组中剔除，剩下未中奖项
        shuffle($list); //打乱数组顺序
        for($i=0;$i<count($list);$i++){
            //$pr[$list[$i]['id']] = $list[$i]['active_title'];
            $pr[] = $list[$i];
        }
        $res['no'] = $pr;
        //halt($res);
        return $res;
    }
    
}
